<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/12/22 Time: 17:20
 */

namespace App\Lib\Utils;

use App\Models\Promotion\Coupon as Coup;

class Coupon
{
    /**
     * @var array 产品列表
     */
    protected $products = [];

    /**
     * @var int 订单总价
     */
    private $total_price = 0;

    /**
     * @var int 减少的价格
     */
    private $reduce_price = 0;

    /**
     * @var array 优惠券信息
     */
    private $coupons = [];

    public function __construct($products, $total_price, $coupon_id = [])
    {
        $this->total_price = $total_price;
        $this->products = $products;
        $this->coupon($coupon_id);
    }


    public function getCoupon()
    {
        return [
            'reduce_price' => $this->reduce_price,
            'coupons' => $this->coupons
        ];
    }

    /**
     *
     * 使用优惠券
     * author Paco
     * createTime 2018/12/22 15:48
     */
    private function coupon($coupon_id)
    {
        $time = time();
        $coupons = Coup::query()->where('start_time', '<=', $time)
            ->where('end_time', '>=', $time)
            ->when($coupon_id, function ($query) use ($coupon_id) {
                $query->whereIn('coupon_id', $coupon_id);
            })->get();

        if ($coupons) {
            foreach ($coupons as $coupon) {
                foreach ($this->products as $product) {
                    $this->selectType($coupon, $product);
                }

            }
        }
    }

    private function selectType($conpon, $product)
    {
        switch ($conpon->type) {
            case 1:
                $this->order($conpon);
                break;
            case 2:
                $this->good($conpon, $product);
                break;
            case 3:
                $this->category($conpon, $product);
                break;
            case 4:
                $this->brand($conpon, $product);
                break;
            case 5:
                $this->seller($conpon, $product);
                break;
        }
    }

    private function selectAwardType($conpon)
    {
        switch ($conpon->award_type) {
            case 1:
                $this->reducePrice($conpon);
                break;
            case 2:
                $this->fullReduce($conpon);
                break;
        }
    }


    private function order($coupon)
    {
        $this->selectAwardType($coupon);

    }


    private function good($conpon, $product)
    {
        $pro_ids = explode(',', $conpon->condition);
        if (in_array($product['product_id'], $pro_ids)) {
            $this->selectAwardType($conpon);
        }
    }

    private function category($conpon, $product)
    {
        $category_ids = explode(',', $conpon->condition);
        if (in_array($product['cat_id'], $category_ids)) {
            $this->selectAwardType($conpon);
        }
    }


    private function brand($conpon, $product)
    {
        $brand_ids = explode(',', $conpon->condition);
        if (in_array($product['brand_id'], $brand_ids)) {
            $this->selectAwardType($conpon);
        }
    }


    private function seller($conpon, $product)
    {
        $seller_ids = explode(',', $conpon->condition);
        if (in_array($product['seller_id'], $seller_ids)) {
            $this->selectAwardType($conpon);
        }
    }


    /**
     *
     * 减价
     * author Paco
     * createTime 2018/12/24 14:30
     * @param $coupon
     * @param bool $full
     * @return int
     */
    private function reducePrice($coupon): int
    {
        $this->reduce_price = $coupon->award_value;
        $this->coupons[] = $coupon;
    }


    /**
     *
     * 满减
     * author Paco
     * createTime 2018/12/24 18:27
     * @param $coupon
     */
    private function fullReduce($coupon)
    {
        if ($this->total_price >= $coupon->condition) {
            $this->reduce_price = $coupon->award_value;
            $this->coupons[] = $coupon;
        }
    }


}